package jspservlet.servlet;

import java.io.IOException;
//import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import jspservlet.dao.InfoDAO;
import jspservlet.dao.UserDAO;
//import jspservlet.dao.impl.InfoDAOImpl;
import jspservlet.dao.impl.UserDAOImpl;
import jspservlet.vo.User;

public class RegisServlet extends HttpServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
	}

	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		User user=new User();
		//PrintWriter out = res.getWriter();
		user.setUsername(req.getParameter("phone"));
		user.setPassword(req.getParameter("password"));
		String pwd1 = req.getParameter("password1");

		UserDAO dao = new UserDAOImpl(); 

		int flag = 0;
		if(pwd1.equals(user.getPassword())){
			try {
				flag = dao.registeUser(user);
			} 
			catch (Exception e) {
				e.printStackTrace();
			}
			if(flag == 1){
				res.getWriter().println("<script>alert('Registration Successful!')</script>");
				res.getWriter().println("<script>window.location.href='./index.jsp'</script>");
			}else{
				res.getWriter().println("<script>alert('Registration Failed!')</script>");
				res.getWriter().println("<script>window.location.href='./register.jsp'</script>");
			}
		}else{
			res.getWriter().println("<script>alert('Twice password not equals!')</script>");
			res.getWriter().println("<script>window.location.href='./register.jsp'</script>");
		}	 
	}
}